<script type="text/javascript" >
    stFacebookToolbox.load_tooltip();
    stFacebookToolboxcustom.on_submitform_new('#stFacebookToolboxForm');
    stFacebookToolbox.loadUploadButton('50');
    stFacebookToolbox.loadRemoveButton();
     jQuery(document).ready(function(){
           <?php if($action == 'edit_coupon_template'){?>
                 jQuery('#coupon').show();
                    jQuery('#site_map').hide();
                    jQuery('#gallery_app').hide();
                    jQuery('#inner_content').css('width','87%');
                        jQuery('#inner_content').css('float','right');
                        jQuery('#side-nav').fadeIn('normal');
                        jQuery("#tabs ul li").each(function(){
                        if(jQuery(this).children().attr('action') == 'gallery_app' || jQuery(this).children().attr('action') == 'coupon')
                            jQuery(this).hide();
                        if(jQuery(this).children().attr('action') == 'home_applications')
                            jQuery(this).attr('class','current');
                        });
                        jQuery("a[action='coupon_template']").parent().attr('class','current');
                 <?php }?>
        jQuery("#coupon_limit").keyup(function(){
            var number = (parseInt(jQuery("#coupon_limit").val()) - <?php echo $number_coupon;?>);
            if(number >=0){
                jQuery("#number_left").html(number);
            }
            else {
                jQuery("#coupon_limit").val(<?php echo $number_coupon;?>);
            }
            
        });
        jQuery('#from').datetimepicker({"dateFormat":"dd.mm.yy"});
        jQuery('#to').datetimepicker({"dateFormat":"dd.mm.yy"});
        jQuery("a.add-new-h2").click(function(){
            jQuery("#coupon_name").val(jQuery("#name").val());
           jQuery("#coupon_title").val(jQuery("#title").val());
           jQuery("#coupon_text").val(jQuery("#description").val());
           jQuery("#coupon_second").val(jQuery("#second").val());
           jQuery("#coupon_template").val(jQuery("#graphic").val());
           document.pdf.submit(); 
        });
     });

     function graphic(id){
         var array = new Array();
         <?php 
         foreach($graphic as $row){
             echo "array[{$row->CouponTemplateGraphicsID}] = '{$row->TemplatesGraphicsPath}';";
         }
         ?>
         jQuery("#graphic_image").attr('src','https://s3-ap-southeast-1.amazonaws.com/<?php echo $config['gallery_bucket'];?>/<?php echo 'fb-app-coupon/';?>'+array[id]);
     }
     jQuery("#graphic").change(function(){
        graphic(jQuery("#graphic").val());
     });
     graphic(jQuery("#graphic").val());
          function check_type(check){
         if(check == 2){
             jQuery("#type_hide").slideUp();
             jQuery("#title").val('null');
             jQuery("#description").val('null');
             jQuery("#url_display").slideDown();
         }
         else {
             jQuery("#type_hide").slideDown();
             jQuery("#url_display").slideUp();
         }
     }
     function func_auto_post(check){
         if(check){
             jQuery("#display_autopost").slideDown();
         }
         else {
             jQuery("#display_autopost").slideUp();
         }
     }
</script>
<style>
    #stcontainer .w100p table td {
    border-bottom: 1px solid #FFFFFF;
    padding: 0;
}
#stcontainer .w100p table {
    table-layout: fixed;
    width: auto;
}
</style>
<?php 
$from = explode('-',substr($result[0]->ValidFromDateTime,0,10));
$from = $from[2].'.'.$from[1].'.'.$from[0].' '.substr($result[0]->ValidFromDateTime,11,5);
$to = explode('-',substr($result[0]->ValidToDateTime,0,10));
$date = $to[2].'.'.$to[1].'.'.$to[0];
$to = $to[2].'.'.$to[1].'.'.$to[0].' '.substr($result[0]->ValidToDateTime,11,5);
?>
<form name="pdf" action="<?php echo get_bloginfo('url');?>/coupon/preview.php" method="POST" target="_blank">
        <input type="hidden" name="coupon_name" id="coupon_name" />
    <input type="hidden" name="coupon_title" id="coupon_title" />
    <input type="hidden" name="coupon_text" id="coupon_text" />
    <input type="hidden" name="coupon_second" id="coupon_second" />
    <input type="hidden" name="coupon_template" id="coupon_template" />
    <input type="hidden" name="date" value="<?php echo $date;?>" />
</form>
<div class="w100p bgl">
    <div class="w100p bgr p5b">
        <form class="stform" id="stFacebookToolboxForm" action="<?php echo admin_url('/admin.php?page=' . $config['plugin_name'] . '&action=' . $current_action) ?>" method="post" >
            <ul>
                <li>
                    <label><?php _e('Campaign name', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $result[0]->CouponTemplateName;?>" class="required" name="data[name]" id="name" type="text"   />
                        <input type="hidden" value="<?php echo $result[0]->CouponTemplateID;?>" name="data[id]" />
                       
                    </div>
                    <br clear="both" />
                </li>
                <li style="width:724px;">
                    <label><?php _e('Maximum number of coupons', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $result[0]->coupon_limit;?>" class="required" name="data[coupon_limit]" id="coupon_limit" type="text"  /> 
                       
                    </div> (Coupons left : <span id="number_left"><?php echo ($result[0]->coupon_limit - $number_coupon);?></span>)
                    <br clear="both" />
                </li>
                <li>
                     <label><?php _e('Campaign type', $config['plugin_name']); ?>:</label>
                    <div class="required">
                       <select name="data[campaign_type]" id="campaign_type" onchange="check_type(this.value)">
                           <option value="1" <?php if($result[0]->campaign_type == 1){echo 'selected';}?>>Coupon campaign</option>
                           <option value="2" <?php if($result[0]->campaign_type == 2){echo 'selected';}?>>External URL campaign</option>
                        </select>
                       
                    </div>
                    <br clear="both" />
                    
                </li>
                <li id="url_display" <?php if($result[0]->campaign_type == 1){echo 'style="display:none;"';}?>>
                    <label><?php _e('External URL', $config['plugin_name']); ?>:</label>
                    <div class="">
                        <input value="<?php echo $result[0]->url;?>" class="" name="data[url]" id="url" type="text"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                <div id="type_hide" <?php if($result[0]->campaign_type == 2){echo 'style="display:none;"';}?>>
                <li>
                    <label><?php _e('Coupon Title', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $result[0]->CouponTitle;?>" class="required" name="data[title]" id="title" type="text"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                
                <li>
                    <label><?php _e('Coupon Text', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <textarea name="data[description]" id="description" class="required"><?php echo $result[0]->CouponTemplateDescription;?></textarea>
                       
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Secondary text', $config['plugin_name']); ?>:</label>
                    <div class="">
                        <textarea name="data[second]" id="second"><?php echo $result[0]->CouponSecondTitle;?></textarea>
                       
                    </div>
                    <br clear="both" />
                </li>
                 <li>
                    <label><?php _e('Template graphic', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <select name="data[graphic]" id="graphic" onchange="">
                            <?php 
                            foreach($graphic as $row){
                                $checked = '';
                                if($result[0]->CouponTemplateGraphicsID == $row->CouponTemplateGraphicsID){
                                    $checked = 'selected';
                                }
                                echo "<option value='{$row->CouponTemplateGraphicsID}' $checked>{$row->TemplatesGraphicsName}</option>";
                            }
                            ?>
                        </select>
                        <p></p>
                        <img id="graphic_image" width="110" style="margin-left:3px;"/>
                       
                    </div>
                    <br clear="both" />
                </li>
                </div>
                <li>
                    <label><?php _e('User must be page fan to generate coupon', $config['plugin_name']); ?>:</label>
                    <div class="">
                        <input value="1" class="" name="data[is_fan]" <?php if($result[0]->is_fan == 1){echo 'checked';} ;?> id="is_fan" type="checkbox"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                 <li>
                    <label><?php _e('Allow user to share campaign', $config['plugin_name']); ?>:</label>
                    <div class="">
                        <input value="1" class="" name="data[is_share]" id="is_share"  <?php if($result[0]->is_share == 1){echo 'checked';} ;?> type="checkbox"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Auto post to users wall', $config['plugin_name']); ?>:</label>
                    <div class="">
                        <input value="1" class="" name="data[auto_post]" id="auto_post" <?php if($result[0]->auto_post == 1){echo 'checked';} ?> type="checkbox" onclick="func_auto_post(this.checked);"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                <div id="display_autopost" <?php if($result[0]->auto_post == 0){?>style="display:none;"<?php }?>>
                <li>
                    <label><?php _e('Wall title', $config['plugin_name']); ?>:</label>
                    <div class="">
                        <input value="<?php echo $result[0]->wall_title;?>" class="" name="data[wall_title]" id="wall_title" type="text"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Wall text', $config['plugin_name']); ?>:</label>
                    <div class="">
                        <input value="<?php echo $result[0]->wall_text;?>" class="" name="data[wall_text]" id="wall_text" type="text"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Wall image', $config['plugin_name']); ?>:</label>
                    <div class="">
                          <?php
                        $class_hide = "hide";
                        if (!empty($result[0]->wall_image)) {
                            $upload_data = wp_upload_dir();
                            $picture = explode('wp-content/',$result[0]->wall_image);
                            $picture = '../wp-content/'.$picture[1];
                            $class_hide = "";
                        }
                        ?>
                                <input type="hidden" <?php if($check){echo "value='{$result[0]->wall_image}'";} ?> id="id_upload_file" name="data[upload_file]" class="" value="<?php echo $result[0]->wall_image;?>">
                                <div class="upload_button_div">
                                    <span class="button image_upload_button" id="upload_file">Upload graphics</span>
                                    <span class="button image_reset_button <?php echo $class_hide ?>" id="reset_upload_file" pictureid="upload_file">Remove</span>
                                </div>  
                                <?php
                             if ($result[0]->wall_image != '') {
                                 ?>
                            <img alt="" src="<?php echo str_replace('/var/www/','../',substr($result[0]->wall_image,0,strlen($result[0]->wall_image)-4)).'-thumb'.substr($result[0]->wall_image,-4,4); ?>?cache=<?php echo rand(1,1000); ?>" id="image_upload_file" class="hide preview" style="display: inline;"/>
                        <?php } ?>
                       
                    </div>
                    <br clear="both" />
                </li>
                </div>
                <li>
                    <label><?php _e('Button image', $config['plugin_name']); ?>:</label>
                    <div class="">
                          <?php
                        $class_hide = "hide";
                        if (!empty($result[0]->image_before)) {
                            $upload_data = wp_upload_dir();
                            $picture = explode('wp-content/',$result[0]->image_before);
                            $picture = '../wp-content/'.$picture[1];
                            $class_hide = "";
                        }
                        ?>
                                <input type="hidden" <?php if($check){echo "value='{$result[0]->image_before}'";} ?> id="id_upload_button" name="data[upload_button]" class="" value="<?php echo $result[0]->image_before;?>">
                                <div class="upload_button_div">
                                    <span class="button image_upload_button" id="upload_button">Upload Button</span>
                                    <span class="button image_reset_button <?php echo $class_hide ?>" id="reset_upload_button" pictureid="upload_button">Remove</span>
                                </div>  
                                <?php
                             if ($result[0]->image_before != '') {
                                 ?>
                            <img alt="" src="<?php echo str_replace('/var/www/','../',substr($result[0]->image_before,0,strlen($result[0]->image_before)-4)).'-thumb'.substr($result[0]->image_before,-4,4); ?>?cache=<?php echo rand(1,1000); ?>" id="image_upload_button" class="hide preview" style="display: inline;"/>
                        <?php } ?>
                       
                    </div>
                    <br clear="both" />
                </li>
          
              
                <li>
                    <label><?php _e('Discount type', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <select name="data[type]" id="type">
                            <?php 
                            foreach($type as $row){
                                $checked = '';
                                if($result[0]->DiscountTypeID == $row->DiscountTypeID){
                                    $checked = 'selected';
                                }
                                echo "<option value='{$row->DiscountTypeID}' $checked>{$row->DiscountTypeName}</option>";
                            }
                            ?>
                        </select>
                       
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Discount value', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $result[0]->DiscountValue;?>" class="required" name="data[value]" id="value" type="text"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Valid From', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $from;?>" class="required" name="data[from]" id="from" type="text"  />
                       
                    </div>
                    <br clear="both" />
                </li>
                <li>
                    <label><?php _e('Valid To', $config['plugin_name']); ?>:</label>
                    <div class="required">
                        <input value="<?php echo $to;?>" class="required" name="data[to]" id="to" type="text"  />
                       
                    </div>
                    <br clear="both" />
                </li>
               <li>
                    <label><?php _e('Coupon detail', $config['plugin_name']); ?>:</label>
                    <div class="">
                        
<!--                       <input type="button" value="Display editor" />-->
                        
                    </div>
                    <br clear="both" />
                </li>
                 <?php
                echo $option['editor_content'];
                $functions->customEditor(stripslashes($result[0]->coupon_detail),'editor_content');
                ?>
               
               <li>
                    <input type="submit" id="submitTinyMCE" value="Save"/>
                </li>

          
            </ul>
           
        </form>
    </div>
</div>
